﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using BLL;
using Model;
using Util;
using System.Drawing.Drawing2D;

namespace UI
{
    public partial class QuerryStocksPlaceStartForm : Form
    {
        #region  画货位需要的参数
        /// <summary>
        /// 起始x位置
        /// </summary>
        int x = 50;
        /// <summary>
        /// 起始y位置
        /// </summary>
        int y = 5;
        /// <summary>
        /// 线宽度
        /// </summary>
        int lineHeight = 1;
        /// <summary>
        /// 层高度
        /// </summary>
        int heightCeng = 400;
        /// <summary>
        /// 层宽度
        /// </summary>
        int widthCeng = 1800;
        /// <summary>
        /// 上层下层盒数
        /// </summary>
        int numHe = 14;
        /// <summary>
        /// 下层盒中格数
        /// </summary>
        int numBottomGe = 4;
        /// <summary>
        /// 上下层偏移量
        /// </summary>
        int offsetBottom = 150;
        /// <summary>
        /// 菱形宽
        /// </summary>
        int recWidht = 110;
        /// <summary>
        /// 菱形高
        /// </summary>
        int recHeight = 32;
        /// <summary>
        /// x偏移量
        /// </summary>
        int offsetX = 6;
        #endregion

        WarehouseBLL bllWh = new WarehouseBLL();
        PlaceBLL bll = new PlaceBLL();
        StocksBLL sbll = new StocksBLL();
        LoadingForm loading;

        public QuerryStocksPlaceStartForm()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < this.MdiParent.MdiChildren.Length; i++)
            {
                //判断当前子窗体的Text属性值是否与传入的字符串值相同               
                string tag = string.Empty;
                if (this.MdiParent.MdiChildren[i].Tag != null)
                {
                    tag = this.MdiParent.MdiChildren[i].Tag.ToString().Trim();
                }
                bool flag = this.Text.Trim().Equals(tag);
                if (flag)
                {

                    this.MdiParent.MdiChildren[i].Close();

                }
            }
            this.timer1.Enabled = true;
            this.timer1.Start();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            loading = new LoadingForm();
            Thread thread = new Thread((ThreadStart)delegate
            {
                Application.Run(loading);
            });
            thread.Start();
            Global.ListPlacePos = new List<Util.PlacePos>();
            Image im = setPlace();//数据处理函数耗时比较长
            loading.Invoke((EventHandler)delegate { loading.Close(); });
            QuerryStocksPlaceForm q = new QuerryStocksPlaceForm(im);
            q.MdiParent = this.MdiParent;
            q.Tag = this.Text;
            q.Show();
        }


        /// <summary>
        /// 检测
        /// </summary>
        public Image setPlace()
        {
            ////1查出库，确定高度
            DataTable dtWh = bllWh.GetList(null);
            int numWh = dtWh.Rows.Count;

            //2查层，确定宽度
            DataTable dtCeng = bll.GetCeng();

            Dictionary<string, List<string>> dicCeng = new Dictionary<string, List<string>>();

            for (int i = 0; i < numWh; i++)
            {
                List<string> list = new List<string>();

                for (int j = 0; j < dtCeng.Rows.Count; j++)
                {
                    if (dtCeng.Rows[j][0].ToString().Substring(0, 2).Equals(dtWh.Rows[i][0].ToString()))
                    {
                        list.Add(dtCeng.Rows[j][0].ToString());
                    }
                }

                dicCeng.Add(dtWh.Rows[i][0].ToString(), list);
            }

            int h = heightCeng * numWh;

            int maxNum = 0;
            foreach (var item in dicCeng.Values)
            {
                maxNum = maxNum > item.Count ? maxNum : item.Count;
            }

            int w = widthCeng * maxNum;

            Image im = new Bitmap(w, h);

            try
            {
                //画底图              
                Graphics g = Graphics.FromImage(im);
                SolidBrush wbrush = new SolidBrush(Color.White);
                g.FillRectangle(wbrush, 0, 0, w, h);

                //货位状态
                int status = 0;

                int n = 0;
                foreach (var key in dicCeng.Keys)
                {
                    int ceng = dicCeng[key].Count;

                    int py = y + heightCeng * n;
                    int pyBottom = py + offsetBottom;

                    for (int m = 0; m < dicCeng[key].Count; m++)
                    {
                        int px = x + widthCeng * m;

                        //构造货位编码(盒)
                        for (int i = 0; i < numHe*2; i++)
                        {
                            string he = dicCeng[key][m] + (i + 1).ToString().PadLeft(2, '0');

                            DataTable dt = bll.GetByHe(he);
                            List<PlaceInfo> listSb = new List<PlaceInfo>();

                            if (dt.Rows.Count > 0)
                            {
                                if (dt.Rows.Count == 1)
                                {//只有盒
                                    //是否有货
                                    if (!sbll.isHaveGoods(he))
                                    {//无货
                                        status = 0;
                                    }
                                    else
                                    {
                                        status = 1;
                                    }

                                    listSb.Add(new PlaceInfo(he, status));
                                }
                                else
                                {//有格
                                    //小于7的上层，按实际；大于7，8个
                                    if (i < numHe)
                                    {
                                        for (int j = 0; j < dt.Rows.Count; j++)
                                        {
                                            if (!dt.Rows[j][0].ToString().Equals(he))
                                            {
                                                if (!sbll.isHaveGoods(dt.Rows[j][0].ToString()))
                                                {
                                                    status = 0;
                                                }
                                                else
                                                {
                                                    status = 1;
                                                }

                                                listSb.Add(new PlaceInfo(dt.Rows[j][0].ToString(), status));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        for (int j = 0; j < numBottomGe; j++)
                                        {
                                            string ge = he + (j + 1).ToString().PadLeft(2, '0');
                                            bool isHaveGe = false;
                                            for (int k = 0; k < dt.Rows.Count; k++)
                                            {
                                                if (ge.Equals(dt.Rows[k][0].ToString()))
                                                {
                                                    isHaveGe = true;

                                                    if (!sbll.isHaveGoods(ge))
                                                    {//无货
                                                        status = 0;
                                                    }
                                                    else
                                                    {
                                                        status = 1;
                                                    }

                                                    break;
                                                }
                                            }

                                            if (!isHaveGe)
                                            {
                                                status = 10;
                                            }

                                            listSb.Add(new PlaceInfo(ge, status));
                                        }
                                    }
                                }
                            }
                            else
                            {//无盒
                                status = 10;
                                listSb.Add(new PlaceInfo("未分配", status));
                            }

                            DrawPlace(g, i < numHe, py, px, i, listSb, recWidht, recHeight, offsetX);
                        }

                        //画层名称编码
                        T_JB_Place place = bll.GetById(dicCeng[key][m]);
                        g.DrawString(place.C_name + " " + place.C_id, new Font("宋体", 30), new SolidBrush(Color.Black), new Point(600 + widthCeng * m, 300 + heightCeng * n));

                        //划线
                        Pen pen = new Pen(Color.Black, 1);
                        pen.DashStyle = DashStyle.Custom;
                        pen.DashPattern = new float[] { 5f, 5f };
                        g.DrawLine(
                            pen,
                            new Point(px, py + lineHeight * (numBottomGe - 1) + recHeight * numBottomGe),
                            new Point(px, py + offsetBottom)
                                );
                        g.DrawLine(
                            pen,
                            new Point(px + recWidht * numHe + lineHeight * (numHe - 1), py),
                            new Point(px + recWidht * numHe + lineHeight * (numHe - 1), py + offsetBottom)
                                );
                        g.DrawLine(
                            pen,
                            new Point(px - offsetX * numBottomGe, py + lineHeight * (numBottomGe - 1) + recHeight * numBottomGe),
                            new Point(px - offsetX * numBottomGe, py + lineHeight * (numBottomGe - 1) + recHeight * numBottomGe + offsetBottom)
                                );
                        g.DrawLine(
                            pen,
                            new Point(px - offsetX * numBottomGe + recWidht * numHe + lineHeight * (numHe - 1), py + lineHeight * (numBottomGe - 1) + recHeight * numBottomGe),
                            new Point(px - offsetX * numBottomGe + recWidht * numHe + lineHeight * (numHe - 1), py + lineHeight * (numBottomGe - 1) + recHeight * numBottomGe + offsetBottom)
                                );
                    }

                    n++;
                }

                im.Save(@"D:\1.bmp");
            }
            catch (Exception)
            {

            }
            return im;
        }

        /// <summary>
        /// 画货位
        /// </summary>
        /// <param name="g"></param>
        /// <param name="isTopCeng"></param>
        /// <param name="py"></param>
        /// <param name="px"></param>
        /// <param name="i"></param>
        /// <param name="listSb"></param>
        /// <param name="recWidht"></param>
        /// <param name="recHeight"></param>
        /// <param name="offsetX"></param>
        private void DrawPlace(Graphics g, bool isTopCeng, int py, int px, int i, List<PlaceInfo> listSb, int recWidht, int recHeight, int offsetX)
        {
            offsetX = offsetX * numBottomGe / listSb.Count;
            recHeight = (recHeight * numBottomGe + (numBottomGe - listSb.Count) * lineHeight) / listSb.Count;
            i = isTopCeng ? i : i - numHe;
            py = isTopCeng ? py : py + offsetBottom;
            bool isEnd = false;

            for (int k = 0; k < listSb.Count; k++)
            {
                isEnd = k == listSb.Count - 1;
                g.FillPolygon(listSb[k].Sb, new Point[] { 
                                        new Point(px + recWidht * i -offsetX*k + lineHeight*i, py + lineHeight*k  + recHeight*k), 
                                        new Point(px + recWidht + recWidht * i -offsetX*k + lineHeight*i, py + lineHeight*k +recHeight*k), 
                                        new Point(px + recWidht - offsetX*(k+1) + recWidht * i + lineHeight*i, py + lineHeight*k + recHeight*(1+k) ), 
                                        new Point(px + recWidht * i - offsetX*(k+1) + lineHeight*i, py + lineHeight*k + recHeight*(1+k))
                                    }); //画货位

                g.DrawString("货位：" + listSb[k].Id, new Font("宋体", 10), new SolidBrush(Color.Black), new Point(px + recWidht * i - offsetX * k + lineHeight * i, py + lineHeight * k + recHeight * k + 2));

                if (listSb[k].Status == 1)
                {
                    DataTable dtStock = sbll.queryStocksList(null, null, listSb[k].Id, null,0);
                    Global.ListPlacePos.Add(new PlacePos(
                        px + recWidht * i - offsetX * k + lineHeight * i, 
                        px + recWidht - offsetX * (k + 1) + recWidht * i + lineHeight * i, 
                        py + lineHeight * k + recHeight * k, 
                        py + lineHeight * k + recHeight * (1 + k), 
                        listSb[k].Id,
                        dtStock.Rows[0][1].ToString(),
                        dtStock.Rows[0][2].ToString(),
                        dtStock.Rows[0][3]==DBNull.Value?0:Convert.ToInt32(dtStock.Rows[0][3]),
                        dtStock.Rows[0][4]==DBNull.Value?0:Convert.ToInt32(dtStock.Rows[0][4]),
                        dtStock.Rows[0][5]==DBNull.Value?0:Convert.ToInt32(dtStock.Rows[0][5]),
                        dtStock.Rows[0][6]==DBNull.Value?0:Convert.ToInt32(dtStock.Rows[0][6]),
                        dtStock.Rows[0][7]==DBNull.Value?0:Convert.ToInt32(dtStock.Rows[0][7]),
                        dtStock.Rows[0][11].ToString().Equals("0001")));
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            this.Close();
        }
    }

    class PlaceInfo
    {
        public PlaceInfo(string id, int status)
        {
            Id = id;
            Status = status;
        }

        public string Id { get; set; }
        public int Status { get; set; }
        public SolidBrush Sb
        {
            get
            {
                switch (Status)
                {
                    case 0:
                        return new SolidBrush(Color.FromArgb(102, 205, 170));//无货
                    case 1:
                        return new SolidBrush(Color.FromArgb(255, 165, 0)); //有货
                    default:
                        return new SolidBrush(Color.FromArgb(192, 192, 192)); //不可用
                }
            }
        }
    }
}
